<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>


<%-- <script type="text/javascript" src="<%=request.getContextPath()%>/js/easyui/jquery.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/easyui/locale/easyui-lang-zh_CN.js"></script>
<link rel="stylesheet" type="text/css"
	href="<%=request.getContextPath()%>/js/easyui/themes/default/easyui.css" />
<link rel="stylesheet" type="text/css" href="<%=request.getCon
textPath()%>/js/easyui/themes/icon.css" />
 --%>

<table id="box2" align= "center"></table>

<div id="tb2" style="padding:5px;">
	<div style="margin-bottom:5px;">
		<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="adress_tool.add();">添加</a>
		<a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="adress_tool.edit();">修改</a>
		<a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="adress_tool.remove();">删除</a>
<!-- 		<a href="#" class="easyui-linkbutton" iconCls="icon-save" plain="true" style="display:none;" id="save" onclick="obj2.save2();">保存</a>
		<a href="#" class="easyui-linkbutton" iconCls="icon-redo" plain="true" style="display:none;" id="redo" onclick="obj2.redo2();">取消编辑</a> -->		
	</div>
	<!-- <div style="padding:0 0 0 7px;color:#333;">
		查询帐号：<input type="text" class="textbox" name="username" style="width:110px">
		创建时间从：<input type="text" name="date_from" class="easyui-datebox" editable="false" style="width:110px">
		到：<input type="text" name="date_to" class="easyui-datebox" editable="false" style="width:110px">
		<a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="obj2.search();">查询</a>
	</div> -->
</div>
   
<form id="adress_add" style="margin:0;padding:5px 0 0 25px;color:#333;">
	<p>公司名称：<input type="text" name="company" class="textbox" style="width:200px;"></p>
	<p>详细地址：<input type="textbox" name="adress" class="textbox" style="width:200px;"></p>
	<p>电话号码：<input id="phone" class="textbox" name="phone" style="width:200px;"></p>
	<p>传&nbsp&nbsp&nbsp&nbsp真：<input id="fax" class="textbox" name="fax" style="width:200px;"></p>
	<p>服务电话：<input id="servicephone" class="textbox" name="servicephone" style="width:200px;"></p>
	<p>邮政编码：<input id="zipcode" class="textbox" name="zipcode" style="width:200px;"></p>
</form>
   
<form id="adress_edit" style="margin:0;padding:5px 0 0 25px;color:#333;">
    <input type="hidden" name="id" class="textbox" style="width:200px;">
	<p>公司名称：<input type="text" name="company" class="textbox" style="width:200px;"></p>
	<p>详细地址：<input type="textbox" name="adress" class="textbox" style="width:200px;"></p>
	<p>电话号码：<input id="phone" class="textbox" name="phone" style="width:200px;"></p>
	<p>传&nbsp&nbsp&nbsp&nbsp真：<input id="fax" class="textbox" name="fax" style="width:200px;"></p>
	<p>服务电话：<input id="servicephone" class="textbox" name="servicephone" style="width:200px;"></p>
	<p>邮政编码：<input id="zipcode" class="textbox" name="zipcode" style="width:200px;"></p>
</form>


 <script type="text/javascript">
 
$(function () {  
  $('#box2').datagrid({
        fit : true,
        border : false,
		//width : 1080,
		//url : 'content.json',
		url : 'adress/adressdatagrid',
		title : '地址列表',
		iconCls : 'icon-search',
		striped : true,
		nowrap : true,
		rownumbers : true,
		//singleSelect : true,
		fitColumns : true,
		columns : [[
			{
				field : 'id',
				title : '编号',
				sortable : true,
				width : 100,
				checkbox : true,
			},
			{
				field : 'company',
				title : '公司名称',
				sortable : true,
				width : 150,
				editor : {
					type : 'validatebox',
					options : {
						required : true,
					},
				},
			},
			{
				field : 'adress',
				title : '详细地址',
				sortable : true,
				width : 200,
				editor : {
					type : 'validatebox',
					options : {
						required : true,
					},
				},
			},
			{
				field : 'phone',
				title : '电话号码',
				sortable : true,
				width : 80,
				editor : {
					type : 'validatebox',
					},
			},
			{
				field : 'fax',
				title : '传真',
				sortable : true,
				width : 80,
				editor : {
					type : 'validatebox',
					},
			},
			{
				field : 'servicephone',
				title : '服务电话',
				sortable : true,
				width : 80,
				editor : {
					type : 'validatebox',
					},
			},
			{
				field : 'zipcode',
				title : '邮政编码',
				sortable : true,
				width : 60,
				editor : {
					type : 'validatebox',
					},
			},
		]],
		toolbar : '#tb2',
		pagination : true,
		pageSize : 5,
		pageList : [5, 20, 30],
		pageNumber : 1,
		sortName : 'id',
		sortOrder : 'DESC',
		
		//双击编辑
		onDblClickRow : function (rowIndex, rowData) {
		 	$.ajax({
					url : 'adress/findadress',
					type : 'post',
					data : {
						id : rowData.id,
					},
					beforeSend : function () {
						$.messager.progress({
							text : '正在获取中...',
						});
					},
					success : function (data, response, status) {
						$.messager.progress('close');
						
						if (data) {
							$('#adress_edit').form('load', {
								id : data.id,
								company : data.company,
								phone : data.phone,
								adress : data.adress,
								servicephone : data.servicephone,
								fax : data.fax,
								zipcode : data.zipcode,
								//auth_edit : obj[0].auth,
							}).dialog('open');
							
							
							
						} else {
							$.messager.alert('获取失败！', '未知错误导致失败，请重试！', 'warning');
						}
					}
				});		    
		
			/* if (obj2.editRow != undefined) {
				$('#box2').datagrid('endEdit', obj2.editRow);
			}
		
			if (obj2.editRow == undefined) {
				$('#save,#redo').show();
				$('#box2').datagrid('beginEdit', rowIndex);
				obj2.editRow = rowIndex;
			} */
			
		},
/* 		onAfterEdit : function (rowIndex, rowData, changes) {
			$('#save,#redo').hide();
			
			var inserted = $('#box2').datagrid('getChanges', 'inserted');
			var updated = $('#box2').datagrid('getChanges', 'updated');
			
			var url = info =  '';
			
			//新增用户
			if (inserted.length > 0) {
				url = 'adress/adress_update';
				info = '新增';
			}
			
			//修改用户
			if (updated.length > 0) {
				url = 'adress/adress_update';
				info = '修改';
			}
			
			$.ajax({
				type : 'POST',
				url : url,
				data : {
					id : rowData["id"],
					company : rowData["company"],
					adress : rowData["adress"],
					phone : rowData["phone"],
					fax : rowData["fax"],
					servicephone : rowData["servicephone"],
					zipcode : rowData["zipcode"]
					},
				beforeSend : function () {
					$('#box2').datagrid('loading');
				},
				success : function (data) {
					if (data == "success") {
						$('#box2').datagrid('loaded');
						$('#box2').datagrid('load');
						$('#box2').datagrid('unselectAll');
						$.messager.show({
							title : '提示',
							msg : '地址' + info + '成功！',
						});
						obj2.editRow = undefined;
					}else{
					    $('#box2').datagrid('loaded');
						$('#box2').datagrid('load');
						$('#box2').datagrid('unselectAll');
						$.messager.show({
							title : '提示',
							msg : '用户名重复，'+ info + '失败',
						});
						obj2.editRow = undefined;
					}
				},
			});
			//console.log(rowData);
		}, */
	});
	
	adress_tool = {
		add : function () {
			$('#adress_add').dialog('open');
			$('input[name="company"]').focus();
		},
		edit : function () {
			var rows = $('#box2').datagrid('getSelections');
			if (rows.length > 1) {
				$.messager.alert('警告操作！', '编辑记录只能选定一条数据！', 'warning');
			} else if (rows.length == 1) {
				$.ajax({
					url : 'adress/findadress',
					type : 'post',
					data : {
						id : rows[0].id,
					},
					beforeSend : function () {
						$.messager.progress({
							text : '正在获取中...',
						});
					},
					success : function (data, response, status) {
						$.messager.progress('close');
						
						if (data) {
							$('#adress_edit').form('load', {
								id : data.id,
								company : data.company,
								phone : data.phone,
								adress : data.adress,
								servicephone : data.servicephone,
								fax : data.fax,
								zipcode : data.zipcode,
								//auth_edit : obj[0].auth,
							}).dialog('open');
							
							
							
						} else {
							$.messager.alert('获取失败！', '未知错误导致失败，请重试！', 'warning');
						}
					}
				});
			} else if (rows.length == 0) {
				$.messager.alert('警告操作！', '编辑记录至少选定一条数据！', 'warning');
			}
		},
		remove : function () {
			var rows = $('#box2').datagrid('getSelections');
			if (rows.length > 0) {
				$.messager.confirm('确定操作', '您正在要删除所选的记录吗？', function (flag) {
					if (flag) {
						var ids = [];
						for (var i = 0; i < rows.length; i ++) {
							ids.push(rows[i].id);
						}
						//console.log(ids.join(','));
						$.ajax({
							type : 'POST',
							url : 'adress/deladress',
							data : {
								ids : ids.join(','),
							},
							beforeSend : function () {
								$('#box2').datagrid('loading');
							},
							success : function (data) {
								if (data) {
									$('#box2').datagrid('loaded');
									$('#box2').datagrid('load');
									$('#box2').datagrid('unselectAll');
									$.messager.show({
										title : '提示',
										msg : data + '个地址被删除成功！',
									});
								}
							},
						});
					}
				});
			} else {
				$.messager.alert('提示', '请选择要删除的记录！', 'info');
			}
		},
	};
	
	
	$('#adress_add').dialog({
		width : 400,
		title : '新增管理',
		modal : true,
		closed : true,
		buttons : [{
			text : '提交',
			handler : function () {
				if ($('#adress_add').form('validate')) {
				    var adress = $("#adress_add").serialize();
					$.ajax({
						url : 'adress/adress_update',
						type : 'post',
						data : adress,
						beforeSend : function () {
							$.messager.progress({
								text : '正在新增中...',
							});
						},
						success : function (data, response, status) {
							$.messager.progress('close');
							
							if (data == "success") {
								$.messager.show({
									title : '提示',
									msg : '新增成功',
								});
								$('#adress_add').dialog('close').form('reset');
								$('#box2').datagrid('reload');
							} else {
								$.messager.alert('新增失败！', '未知错误导致失败，请重试！', 'warning');
							}
						}
					});
				}
			},
		},{
			text : '取消',
			iconCls : 'icon-redo',
			handler : function () {
				$('#adress_add').dialog('close').form('reset');
			},
		}],
	});
	$('#adress_edit').dialog({
		width : 400,
		title : '修改管理',
		modal : true,
		closed : true,
		buttons : [{
			text : '提交',
			handler : function () {
				if ($('#adress_edit').form('validate')) {
				    var adress = $("#adress_edit").serialize();
					$.ajax({
						url : 'adress/adress_update',
						type : 'post',
						data : adress,
						beforeSend : function () {
							$.messager.progress({
								text : '正在提交中...',
							});
						},
						success : function (data, response, status) {
							$.messager.progress('close');
							
							if (data == "success") {
								$.messager.show({
									title : '提示',
									msg : '修改成功',
								});
								$('#adress_edit').dialog('close').form('reset');
								$('#box2').datagrid('reload');
							} else {
								$.messager.alert('修改失败！', '未知错误导致失败，请重试！', 'warning');
							}
						}
					});
				}			
			},
		},{
			text : '取消',
			iconCls : 'icon-redo',
			handler : function () {
				$('#adress_edit').dialog('close').form('reset');
			},
		}],
	});
	
});
  


       

    
    </script>

